package com.yu.demo.user.domain.valueobject;

import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.util.regex.Pattern;

/**
 * 用户名值对象
 * 封装用户名的业务规则和验证逻辑
 */
@Getter
@EqualsAndHashCode
public class Username {
    private static final Pattern USERNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_]{3,20}$");
    private final String value;

    public Username(String value) {
        if (value == null || value.trim().isEmpty()) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        if (!USERNAME_PATTERN.matcher(value).matches()) {
            throw new IllegalArgumentException("用户名只能包含字母、数字和下划线，长度3-20位");
        }
        this.value = value.trim();
    }

    @Override
    public String toString() {
        return value;
    }
}
